<?php
class AppController extends Controller
{
	public $view = "Theme";

	public $uses = array('Project');
	public $helpers = array('Javascript', 'Html', 'Form', 'Image', 'PaypalIpn.Paypal');
	public $components = array('obAuth', 'Session', 'Cookie');


	public function beforeFilter()
	{
		Configure::load('Settings');
		$project = $this->Project->get(Configure::read('Settings.projectId'));

		if ($project) {
			$this->theme = $project['Project']['wbskin'];

			if ($this->Session->check('projectId')) {
				if ($this->Session->read('projectId') == Configure::read('Settings.projectId')) {
					$this->Session->delete('projectId');
				}
			}

			$this->Session->write('projectId', Configure::read('Settings.projectId'));
			$this->Session->write('wbtitle', $project['Project']['wbtitle']);
			$this->Session->write('wbbase', $project['Project']['wbbase']);
			$this->Session->write('reply_to', $project['Project']['reply_to']);
			$this->Session->write('logo', $project['Project']['logo']);
		} else {
			return null;
		}
	}

    public function afterPaypalNotification($txnId)
    {
        //Here is where you can implement code to apply the transaction to your app.
        //for example, you could now mark an order as paid, a subscription, or give the user premium access.
        //retrieve the transaction using the txnId passed and apply whatever logic your site needs.

        $transaction = ClassRegistry::init('PaypalIpn.InstantPaymentNotification')->findById($txnId);
        $this->log($transaction['InstantPaymentNotification']['id'], 'paypal');

        //Tip: be sure to check the payment_status is complete because failure transactions
        //     are also saved to your database for review.

        if($transaction['InstantPaymentNotification']['payment_status'] == 'Completed'){
          //Yay!  We have monies!
        } else {
          //Oh no, better look at this transaction to determine what to do; like email a decline letter.
        }
    }
}
?>